/**
 * @author jiagnqifan
 * @since 2016-03-31
 */
var Models = require('../../../models');
var logger = require('log4js').getLogger();
var EventProxy = require('eventproxy');
var build_types = require('./build_types');
var monitor = require('./build_monitor.js');
  monitor.start();

var Build = Models.Build;

exports.start = function(build_id, callback){

  var ep = new EventProxy();

  ep.fail(function (err) {
      logger.error(err);
      if(callback){
        return callback(err.err || err.msg);
      }else {
        var message = '[can not handle error] '+err.msg;
        logger.error(message);
        logger.error(err);
      }
  });

  Build.findById(build_id).populate('config').exec(function(err, build){
    if(err || !build){
      return ep.throw({err:err,msg:'can not find build:'+build_id});
    }

      console.log('build:'+build);
    var type = build.config.type;
    var policy = build_types.find(type);
    if(!policy){
        return ep.throw('can not handle type:'+type);
    }

    policy.start(build, callback);

  });

}


function ready_build(build, callback){

}


exports.getBuildList = function(cb){

}

exports.getProgress = function(id, cb){

}

exports.cancel = function(id, cb){

}
